import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;

/**
 * 该类表示每一格棋子
 * 属性
 * 坐标【x, y】
 * 0表示没有棋子， 1表示白棋， -1表示黑棋
 * 当前棋子的路径
 * 当前棋子在二维数组中的位置
 * 方法
 * 画格子
 * 落子
 */

public class Node {
    private final int  x;
    private final int y;
    private int value;
    private Image img;
    private static Image blackPieceImage;
    private static Image whitePieceImage;

    static {
        {
            try {
                blackPieceImage = ImageIO.read(new File(Static.Src_B));
                whitePieceImage = ImageIO.read(new File(Static.Src_w));
            } catch (IOException e) {
                e.printStackTrace();
            }

    }
    }

    public Node(int x, int y){
        this.y = y + Static.tool_H;
        this.x = x + Static.tool_H;
        value = 0;
    }

    public int getX() {
        return x;
    }


    public int getY() {
        return y;
    }

    public int getValue() {
        return value;
    }


    public void setValue(int value) {
        this.value = value;
        if (value == 1) {
            img = whitePieceImage;

        }
        else if (value == -1){
            img = blackPieceImage;

        }
    }
    /**
     * 画出自己的格子
     * @param g
     */
    public void drawNode(Graphics g){
        g.setColor(Color.black);
        g.drawRect(x, y, Static.Node_w, Static.Node_w);
    }

    /**
     *判断是否落子
     * @param x  点击的横坐标
     * @param y  点击的纵坐标
     * @return
     */
    public boolean clickNode(int x, int y){
        int start_X = this.x - Static.Radius;
        int end_X = this.x + Static.Radius;
        int start_Y = this.y - Static.Radius;
        int end_Y = this.y + Static.Radius;
//        System.out.println(String.format("x坐标的位置为:%d——%d",start_X, end_X));
//        System.out.println(String.format("y坐标的位置为:%d——%d", start_Y, end_Y));
//        System.out.println(String.format("点的坐标为:%d,%d",x, y));
        if (x >= start_X && x <= end_X && y >= start_Y && y <= end_Y){
            return true;
        }
        else{
//            System.out.println("超出范围");
        }

        return false;
    }

    /**
     *
     * @param g  画笔
     */
    public void playNode(Graphics g){
        g.drawImage(img,this.x - Static.piece_W/2, this.y - Static.piece_W/2, Static.piece_W, Static.piece_W, null );
    }
}
